let express = require("express");
let news = express.Router();
let request = require("request");
var Entities = require("html-entities").XmlEntities;
entities = new Entities();
let cheerio = require("cheerio");
news.get("/newsLatest", (req, res) => {
  request("http://news.mtime.com/", function (error, response, body) {
    if (!error && response.statusCode == 200) {
      let $ = cheerio.load(body); //当前的$符相当于拿到了所有的body里面的选择器
      let items = $("#newslist li .news-cont");
      let news = [];
      items.each((index, item) => {
        let a = item.children[0];
        let imgTag = a.children[0];
        let createTime = item.children[3].children[0].data;
        let href = a.attribs;
        let img = imgTag.attribs;
        let newsSrc = href.href;
        let imgSrc = img.src;
        let title = img.title;
        let newItem = { imgSrc,title,newsSrc,createTime};
        // let newItem = {href,imgsrc};
        // console.log(createTime)
        news.push(newItem);
      });
      res.send(news);
      // console.log(news)
    }
  });
});

news.get("/newsTV", (req, res) => {
  request("http://news.mtime.com/tv/all/#nav", function (error, response, body) {
    if (!error && response.statusCode == 200) {
      let $ = cheerio.load(body); //当前的$符相当于拿到了所有的body里面的选择器
      // let image = $('#newslist li .news-cont > a img').attr('src');
      let items = $("#newslist li .news-cont");
      let news = [];
      items.each((index, item) => {
        let a = item.children[0];
        let imgTag = a.children[0];
        let createTime = item.children[3].children[0].data;
        let href = a.attribs;
        let img = imgTag.attribs;
        let newsSrc = href.href;
        let imgSrc = img.src;
        let title = img.title;
        let newItem = { imgSrc,title,newsSrc,createTime};
        // let newItem = {href,imgsrc};
        news.push(newItem);
      });
      res.send(news);
    }
  });
});
news.get("/newsMusic", (req, res) => {
  request("http://news.mtime.com/movie/all/#nav", function (error, response, body) {
    if (!error && response.statusCode == 200) {
      let $ = cheerio.load(body); //当前的$符相当于拿到了所有的body里面的选择器
      // let image = $('#newslist li .news-cont > a img').attr('src');
      let items = $("#newslist li .news-cont");
      let news = [];
      items.each((index, item) => {
        let a = item.children[0];
        let imgTag = a.children[0];
        let createTime = item.children[3].children[0].data;
        let href = a.attribs;
        let img = imgTag.attribs;
        let newsSrc = href.href;
        let imgSrc = img.src;
        let title = img.title;
        let newItem = { imgSrc,title,newsSrc,createTime};
        // let newItem = {href,imgsrc};
        news.push(newItem);
      });
      res.send(news);
    }
  });
});
news.get("/newsIndustry", (req, res) => {
  request("http://news.mtime.com/people/all/#nav", function (error, response, body) {
    if (!error && response.statusCode == 200) {
      let $ = cheerio.load(body); //当前的$符相当于拿到了所有的body里面的选择器
      // let image = $('#newslist li .news-cont > a img').attr('src');
      let items = $("#newslist li .news-cont");
      let news = [];
      items.each((index, item) => {
        let a = item.children[0];
        let createTime = item.children[3].children[0].data;
        let imgTag = a.children[0];
        let href = a.attribs;
        let img = imgTag.attribs;
        let newsSrc = href.href;
        let imgSrc = img.src;
        let title = img.title;
        let newItem = { imgSrc,title,newsSrc,createTime};
        // let newItem = {href,imgsrc};
        news.push(newItem);
      });
      res.send(news);
    }
  });
});
// news.get('/news/detail',(req,res)=>{
//   console.log(req.query)
//   request()
// })
module.exports = { news };
